const { GoodsModel } = require('../db/model')



const category = async (req, res) => {
    const data = await GoodsModel.find()
    // console.log(data);

    const arr = []

    data.forEach(item => arr.push(item.category))
    // console.log([...new Set(arr)]);
    const result = [...new Set(arr)]
    // return [ ...new Set(arr) ]
    res.send({
        code: 1,
        message: '获取分类列表成功',
        result
    })
}
const getList = async (req, res) => {
    const { current, pagesize, search, sale_type, sortType, category } = req.query
    var { sortMethod, filter } = req.query
    const start = (current - 1) * pagesize
    const info = { title: new RegExp(search) }
    const obj = {}
    sortMethod = sortMethod === 'ASC' ? 1 : -1
    info.sale_type = sale_type === '' ? { $ne: null } : (sale_type * 10) + '%'
    info.category = category === '' ? { $ne: null } : category
    if (sortType === '_id') {
        obj._id = sortMethod
    } else if (sortType === 'current_price') {
        obj.current_price = sortMethod
    } else if (sortType === 'sale_type') {
        obj.sale_type = sortMethod
    }
    if (filter === 'hot') info.is_hot = true
    if (filter === 'sale') info.sale_type = { $ne: '100%' }
    const result = await GoodsModel.find(info).sort(obj).skip(start).limit(pagesize)
    const total = (await GoodsModel.find(info)).length

    
    // if (category && !result.some(item => item === category)) return res.send({
    //     message: '获取商品列表失败, 没有你所选择的分类',
    //     code: 0,
    //     tips: `你传递给我的 'category' 信息是 : ${category}`
    // })
    res.send({
        code: 1,
        message: '获取成功',
        list: result,
        total:Math.ceil(total / pagesize),
        current,
        info,
        obj
    })
}
// const getList = async (req, res) => {
//         const { current, pagesize, search, sale_type, sortType, category } = req.query
//         let { sortMethod, filter } = req.query
//         const start = (current - 1) * pagesize

//         const info = { title: new RegExp(search) }
//         const obj = {}
//         sortMethod = sortMethod === 'ASC' ? 1 : -1
//         info.sale_type = sale_type === '' ? { $ne: null } : (sale_type * 10) + '%'
//         info.category = category === '' ? { $ne: null } : category

//         if (sortType === '_id') {
//             obj._id = sortMethod
//         } else if (sortType === 'current_price') {
//             obj.current_price = sortMethod
//         } else if (sortType === 'sale_type') {
//             obj.sale_type = sortMethod
//         }

//         if (filter === 'hot') info.is_hot = true
//         if (filter === 'sale') info.sale_type = { $ne: '100%' }

//         var result = await GoodsDataModel.find(info).sort(obj).skip(start).limit(pagesize)
//         var total = (await GoodsDataModel.find(info)).length

//         console.log(info, sortType, obj);

//         res.send({
//             code: 1,
//             message: '商品列表请求成功',
//             list: result,
//             total: Math.ceil(total / pagesize)
//         })
//     }
module.exports = {
    category,
    getList
}